package com.yj.ccs.abs.web.asset.mapper;

import com.yj.ccs.abs.web.asset.entity.Contract;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/20
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface ContractMapper {

    @Options(useGeneratedKeys = true, keyProperty = "item.id")
    @Insert("INSERT INTO `contract` (asset_id, buyer, seller, number, goods_type, transport_type, " +
            "amount, unit_price, supply_start_date, supply_end_date, sign_date, expire_date, " +
            "consigned, consignee, delivered, deliverer, reckoned, reckoner, type, create_time)" +
            "VALUES (#{item.asset_id}, #{item.buyer}, #{item.seller}, #{item.number}, " +
            "#{item.goods_type}, #{item.transport_type}, #{item.amount}, #{item.unit_price}, " +
            "#{item.supply_start_date}, #{item.supply_end_date}, #{item.sign_date}, #{item.expire_date}, " +
            "#{item.consigned}, #{item.consignee}, #{item.delivered}, #{item.deliverer}, " +
            "#{item.reckoned}, #{item.reckoner}, #{item.type}, NOW())")
    void create(@Param("item") Contract item);

    @Update("UPDATE `contract` SET buyer = #{item.buyer}, seller = #{item.seller}, number = #{item.number}, goods_type = #{item.goods_type}, " +
            "transport_type = #{item.transport_type}, amount = #{item.amount}, unit_price = #{item.unit_price}, " +
            "supply_start_date = #{item.supply_start_date}, supply_end_date = #{item.supply_end_date}, sign_date = #{item.sign_date}, " +
            "expire_date = #{item.expire_date}, consigned = #{item.consigned}, consignee = #{item.consignee}, delivered = #{item.delivered}, " +
            "deliverer = #{item.deliverer}, reckoned = #{item.reckoned}, reckoner = #{item.reckoner} " +
            "WHERE id = #{item.id}")
    void update(@Param("item") Contract item);

    @Select("SELECT * FROM `contract` WHERE id = #{id} LIMIT 1")
    Contract getById(@Param("id") Long id);

    @Select("SELECT * FROM `contract` WHERE asset_id = #{asset_id} AND type = #{type}")
    Contract getByAssetIdAndType(@Param("asset_id") Long asset_id, @Param("type") Integer type);
}
